#ifndef PARSER
#define PARSER
#include <iostream>
#include <map>
//#include "../Common/any.hpp"
#include <boost/any.hpp>
#include "../TemplateParser/BO.h"
using namespace std;
using namespace TemplateParser;
//using namespace cdiggins;
class Parser
{
	public:
		static map<string, boost::any> ViewData;
		virtual string parse(string)=0;
		virtual string getViewData(string)=0;
		virtual boost::any getViewAnyData(string)=0;
		virtual void setViewData(string,boost::any)=0;
		virtual bool isFunction(string)=0;
		virtual string parseFunction(string)=0;
		virtual bool isOperation(string)=0;
		virtual string parseOperation(string)=0;
		virtual bool isCondition(string)=0;
		virtual bool parseCondition(string)=0;

		string removespace(string);
		string removequote(string);
		bool isstring(string);
		string removeCurlyBracket(string);
		string getDirection(string);
	private:

 };
map<string, boost::any> Parser::ViewData;

	string Parser::getDirection(string in){
		if(in.size()>0){
			string::size_type pos=string::npos;
			pos=in.find_first_of('(');
			if(pos!=string::npos)in=in.substr(0,pos);
		}
		return in;
    }

    string Parser::removespace(string in){
    	return in;
    }
    string Parser::removeCurlyBracket(string in){
    	//if have ()
		if(in.size()>0 && in.at(0)=='(' && in.at(in.size()-1)==')'){
			//check if is not ()&&() then remove
			string::iterator it = in.begin()+1;
			int open_bracket_count=0;
			while(it!=in.end()-1){
				if(*it=='(')open_bracket_count++;
				else if(*it==')')open_bracket_count--;
				if(open_bracket_count<0)break;
				it++;
			}
			if(it==in.end()-1)in=in.substr(1,in.size()-2);
		}
		return in;
    }
    string Parser::removequote(string in){
    	if(in.size()>0 && in.at(0)=='"' && in.at(in.size()-1)=='"'){
    		in=in.substr(1,in.size()-2);
    		//replace \" -> "
    		string::iterator it = in.begin();
    		while(it!=in.end()){
    			if(*it=='\\' && *(it+1)=='"')in.replace(it,it+1,"");
    			it++;
    		}
    		return StringProcessor::atoa(in);
    	}
    	else return in;
    }

    bool Parser::isstring(string in){
    	//if have ""
    	//cout<<in<<endl;
		if(in.size()>0 && in.at(0)=='"' && in.at(in.size()-1)=='"'){
			//check if is not ""+"" then remove
			string::iterator it = in.begin()+1;
			while(it!=in.end()-1){
				if(*it=='"' && *(it-1)!='\\' )break;
				it++;
			}
			if(it==in.end()-1 && *it=='"')return true;
		}
		return false;
    }


#endif
